<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">
			<u-form-item label="入库单别">
				<u-input v-model="dataForm.tf001" disabled />
			</u-form-item>
			<u-form-item label="入库单号">
				<u-input v-model="dataForm.tf002" disabled />
			</u-form-item>
			<u-form-item label="单据日期">
				<u-input v-model="dataForm.tf012" disabled />
			</u-form-item>
			<u-form-item label="入库日期">
				<u-input v-model="dataForm.tf003" disabled />
			</u-form-item>
			<u-form-item label="工作中心">
				<u-input v-model="dataForm.tf011" disabled />
			</u-form-item>
			<u-form-item label="工厂">
				<u-input v-model="dataForm.tf004" disabled />
			</u-form-item>
			<u-form-item label="部门">
				<u-input v-model="dataForm.tf016" disabled />
			</u-form-item>
			<u-form-item label="自动扣料">
				<u-input v-model="dataForm.tf009" disabled />
			</u-form-item>
			<u-form-item label="生成分录">
				<u-input v-model="dataForm.tf010" disabled />
			</u-form-item>
			<u-form-item label="签核状态码">
				<u-input v-model="dataForm.tf014" disabled />
			</u-form-item>
			<u-form-item label="审核者">
				<u-input v-model="dataForm.tf013" disabled />
			</u-form-item>
			<u-form-item label="附件">
				<jnpf-file v-model="dataForm.attachmentUpload" disabled />
			</u-form-item>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item,i) in dataForm.moctgList"
					:key="i">
					<u-form-item label="序号" style="background-color: aqua;">
						{{i+1}}
					</u-form-item>
					<u-form-item label="工单单别">
						<u-input v-model="item.tg014" disabled />
					</u-form-item>
					<u-form-item label="工单单号">
						<u-input v-model="item.tg015" disabled />
					</u-form-item>
					<u-form-item label="产品品号">
						<u-input v-model="item.tg004" disabled />
					</u-form-item>
					<u-form-item label="品名">
						<u-input v-model="item.tg005" disabled />
					</u-form-item>
					<u-form-item label="规格">
						<u-input v-model="item.tg006" disabled />
					</u-form-item>
					<u-form-item label="单位">
						<u-input v-model="item.tg007" disabled />
					</u-form-item>
					<u-form-item label="入/出别">
						<span v-if="item.tg009==1">入</span>
						<span v-else>出</span>
					</u-form-item>
					<u-form-item label="仓库">
						<u-input v-model="item.tg010" disabled />
					</u-form-item>
					<u-form-item label="入库数量">
						<u-input v-model="item.tg011" />
					</u-form-item>
					<u-form-item label="验收数量">
						<u-input v-model="item.tg013" />
					</u-form-item>
					<u-form-item label="报废数量">
						<u-input v-model="item.tg012" />
					</u-form-item>
					<u-form-item label="破坏数量">
						<u-input v-model="item.tg032" />
					</u-form-item>
					<u-form-item label="验退数量">
						<u-input v-model="item.tg023" />
					</u-form-item>
					<u-form-item label="检验状态">
						<u-input v-model="item.tg016" disabled />
					</u-form-item>
					<u-form-item label="急料">
						<u-input v-model="item.tg024" disabled />
					</u-form-item>
					<u-form-item label="批号">
						<u-input v-model="item.tg017" disabled />
					</u-form-item>
					<u-form-item label="批号说明">
						<u-input v-model="item.tg031" />
					</u-form-item>
					<u-form-item label="生产日期">
						<u-input v-model="item.tg030" disabled />
					</u-form-item>
					<u-form-item label="有效日期">
						<u-input v-model="item.tg018" disabled />
					</u-form-item>
					<u-form-item label="复检日期">
						<u-input v-model="item.tg019" disabled />
					</u-form-item>
					<u-form-item label="报废码">
						<u-input v-model="item.tg035" disabled />
					</u-form-item>
					<u-form-item label="备注">
						<u-input v-model="item.tg020" />
					</u-form-item>
					<u-form-item label="项目编号">
						<u-input v-model="item.tg021" disabled />
					</u-form-item>
					<u-form-item label="sQMS检验码">
						<u-input v-model="item.tgs01" disabled />
					</u-form-item>
					<u-form-item label="sQMS检验单号">
						<u-input v-model="item.tgs02" disabled />
					</u-form-item>
					<u-form-item label="包材品号">
						<u-input v-model="item.udf01" disabled />
					</u-form-item>
					<u-form-item label="包材品名">
						<u-input v-model="item.udf02" />
					</u-form-item>
					<u-form-item label="包材数量">
						<u-input v-model="item.udf54" />
					</u-form-item>
					<u-form-item label="模号">
						<u-input v-model="item.udf03" />
					</u-form-item>
					<u-form-item label="已生产量">
						<u-input v-model="item.udf51" />
					</u-form-item>
					<u-form-item label="未生产量">
						<u-input v-model="item.udf52" />
					</u-form-item>
					<u-form-item label="可生产量">
						<u-input v-model="item.udf53" />
					</u-form-item>
				</view>
			</view>
			<u-form-item label="总入库数量">
				<u-input v-model="dataForm.tf023" disabled />
			</u-form-item>
			<u-form-item label="总报废数量">
				<u-input v-model="dataForm.tf025" disabled />
			</u-form-item>
			<u-form-item label="总破坏数量">
				<u-input v-model="dataForm.tf026" disabled />
			</u-form-item>
			<u-form-item label="总验收数量">
				<u-input v-model="dataForm.tf024" disabled />
			</u-form-item>
			<u-form-item label="总验退数量">
				<u-input v-model="dataForm.tf027" disabled />
			</u-form-item>
			<u-form-item label="备注">
				<u-input v-model="dataForm.tf005" />
			</u-form-item>
		</u-form>


	</view>
</template>

<script>
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from '@/api/apply/order'
	import {
		getMoctf,
	} from '@/api/workFlow/flowEngine'
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo
	} from '@/api/common'
	export default {
		mixins: [comMixin],
		computed: {
			// getTb019Computed() {
			// 	return (id) => {
			// 		const options = this.options.filter((item) => id.includes(item.id));
			// 		return options.length > 0 ? options[0].fullName : '';
			// 	};
			// }
		},
		watch: {
			"dataForm.moctgList": {
				handler(newVal, oldVal) {
					this.calculatePrice();
				},
				deep: true,
			},
			//总需领料量，总未领料量，总领料量
			// "dataForm.mocteList": {
			// 	handler(newVal, oldVal) {
			// 		let sumNeed = 0;
			// 		let sumNot = 0;
			// 		let sumTc029 = 0;
			// 		for (let i = 0; i < newVal.length; i++) {
			// 			const e = newVal[i];
			// 			sumNeed += parseFloat(e.ctb_Tb004); //需领用量
			// 			sumNot += parseFloat(e.ctb_Tb004005); //未领用量
			// 			sumTc029 += parseFloat(e.te005); //总领料量
			// 		}
			// 		this.dataForm.sumNeed = sumNeed;
			// 		this.dataForm.sumNot = sumNot;
			// 		this.dataForm.tc029 = sumTc029;
			// 	},
			// 	deep: true,
			// },
		},
		data() {
			return {
				show: false,
				dataForm: {},
				id: '',
				showForm: [],
				formOperatesList: [],
				options: [{
						fullName: "必选，唯一",
						id: "1"
					},
					{
						fullName: "必选，至少一个",
						id: "2"
					},
					{
						fullName: "可选，唯一",
						id: "3"
					},
					{
						fullName: "任意选",
						id: "4"
					},
				],
			}
		},
		methods: {
			init(data, id, flowFormContentJson) {
				console.log('data', data);
				this.setting = data
				this.formOperatesList = data.formOperates
				this.id = id
				this.getInfoFn(data)
			},
			getInfoFn(data) {
				var list = data.id.split("-");

				let obj = {
					tf001: list[1],
					tf002: list[2]
				}
				getMoctf(obj).then(res => {
					if (res.code == 200) {
						this.dataForm = res.data;
						this.getDataInterfaceDataInfotf011(this.dataForm.tf011)
						this.getDataInterfaceDataInfotf004(this.dataForm.tf004)
						this.getDataInterfaceDataInfotf016(this.dataForm.tc005)
						this.getDataInterfaceDataInfock()
					}
				})
			},
			//汇总
			calculatePrice() {
				this.dataForm.tf023 = 0;
				this.dataForm.tf024 = 0;
				this.dataForm.tf025 = 0;
				this.dataForm.tf026 = 0;
				this.dataForm.tf027 = 0;
				for (let i = 0; i < this.dataForm.moctgList.length; i++) {
					//总入库数量
					this.dataForm.tf023 += parseFloat(this.dataForm.moctgList[i].tg011);
					//总验收数量
					this.dataForm.tf024 += parseFloat(this.dataForm.moctgList[i].tg013);
					//总报废数量
					this.dataForm.tf025 += parseFloat(this.dataForm.moctgList[i].tg012);
					//总破坏数量
					this.dataForm.tf026 += parseFloat(this.dataForm.moctgList[i].tg032);
					//总验退数量
					this.dataForm.tf027 += parseFloat(this.dataForm.moctgList[i].tg023);
				}
			},
			queryChildBachelor(tb004) {
				let query = {
					tb004
				};
				BomcbListInit(query).then((res) => {
					this.showForm = res.data;
					this.show = true;
				})
			},
			// 	工作中心
			getDataInterfaceDataInfotf011(id) {
				let data = {
					id,
					interfaceId: '362117292839934533',
					propsValue: 'MD001',
					relationField: 'MD001',
				}
				getDataInterfaceDataInfo('362117292839934533', data).then(res => {
					this.dataForm.tf011 = res.data.JoinStr
				})
			},
			// 	工厂名称
			getDataInterfaceDataInfotf004(id) {
				console.log('部门编号', id);
				let data = {
					id,
					interfaceId: '362111000289221189',
					propsValue: 'MB001',
					relationField: 'MB001',
				}
				console.log('部门编号data', data);
				getDataInterfaceDataInfo('362111000289221189', data).then(res => {
					this.dataForm.tf004 = res.data.JoinStr
				})
			},
			// 	部门
			getDataInterfaceDataInfotf016(id) {
				let data = {
					id,
					interfaceId: '362111557984852549',
					propsValue: 'ME001',
					relationField: 'ME001',
				}
				getDataInterfaceDataInfo('362111557984852549', data).then(res => {
					this.dataForm.tf016 = res.data.JoinStr
				})
			},
			// 	仓库  /dev/api/system/DataInterface/362399109623061061/Action/Info?n=1724377499&id=HX4055C&interfaceId=362399109623061061&propsValue=MB001&relationField=MB001
			getDataInterfaceDataInfock() {
				let list = this.dataForm.moctgList
				for (let i = 0; i < list.length; i++) {
					let data = {
						id: list[i].tg010,
						interfaceId: '362114335423601221',
						propsValue: 'MC001',
						relationField: 'MC001',
					}
					getDataInterfaceDataInfo('362114335423601221', data).then(res => {
						this.dataForm.moctgList[i].tg010 = res.data.MC002
					})
				}

			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>